package cn.iocoder.yudao.module.system.service.goal;

import cn.iocoder.yudao.module.system.controller.admin.goal.vo.StudentMajorGroupGoalCreateReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.goal.StudentMajorGroupGoalDO;
import cn.iocoder.yudao.module.system.dal.mysql.goal.StudentMajorGroupGoalMapper;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 学生目标专业组 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class StudentMajorGroupGoalServiceImpl implements StudentMajorGroupGoalService {

    @Resource
    private StudentMajorGroupGoalMapper studentMajorGroupGoalMapper;

    @Override
    public Long createMajorGroupGoal(StudentMajorGroupGoalCreateReqVO createReqVO, Long userId) {
        // 校验是否已经存在
        if (isMajorGroupGoalExists(userId, createReqVO.getRecruitCode(), createReqVO.getUniversityMajorGroup(), createReqVO.getYear())) {
             throw exception(STUDENT_MAJOR_GROUP_GOAL_EXISTS);
        }

        // 插入
        StudentMajorGroupGoalDO studentMajorGroupGoal = BeanUtils.toBean(createReqVO, StudentMajorGroupGoalDO.class);
        studentMajorGroupGoal.setUserId(userId);
        studentMajorGroupGoalMapper.insert(studentMajorGroupGoal);
        return studentMajorGroupGoal.getId();
    }

    @Override
    public void deleteMajorGroupGoal(Long id, Long userId) {
        // 校验存在
        StudentMajorGroupGoalDO goal = studentMajorGroupGoalMapper.selectById(id);
        if (goal == null) {
            throw exception(STUDENT_MAJOR_GROUP_GOAL_NOT_EXISTS);
        }
        if (!goal.getUserId().equals(userId)) {
             // 实际上如果是admin，可能不需要这个check，但这里是"学生"接口
             // 暂时用通用异常或NOT_EXISTS掩盖
             throw exception(STUDENT_MAJOR_GROUP_GOAL_NOT_EXISTS);
        }
        // 删除
        studentMajorGroupGoalMapper.deleteById(id);
    }

    @Override
    public List<StudentMajorGroupGoalDO> getMajorGroupGoalList(Long userId) {
        return studentMajorGroupGoalMapper.selectListByUserId(userId);
    }

    @Override
    public List<StudentMajorGroupGoalDO> getMajorGroupGoalList(Collection<Long> userIds) {
        return studentMajorGroupGoalMapper.selectListByUserIds(userIds);
    }

    @Override
    public boolean isMajorGroupGoalExists(Long userId, String recruitCode, String majorGroup, Integer year) {
        StudentMajorGroupGoalDO goal = studentMajorGroupGoalMapper.selectByUserIdAndRecruitCodeAndMajorGroup(userId, recruitCode, majorGroup, year);
        return goal != null;
    }

    @Override
    public void updateMajorGroupGoalSort(Long id, Integer sort, Long userId) {
        // 校验存在
        StudentMajorGroupGoalDO goal = studentMajorGroupGoalMapper.selectById(id);
        if (goal == null) {
            throw exception(STUDENT_MAJOR_GROUP_GOAL_NOT_EXISTS);
        }
        if (!goal.getUserId().equals(userId)) {
            throw exception(STUDENT_MAJOR_GROUP_GOAL_NOT_EXISTS);
        }
        // 更新
        studentMajorGroupGoalMapper.updateById(StudentMajorGroupGoalDO.builder().id(id).sort(sort).build());
    }
}
